#include "getHtml.h"
#include "pubfunc.h"
#include "parserHtml.h"


getHtml::getHtml(void)
{
    m_sPageHtml = NULL;
    m_UrlPath = NULL;
   // memset(m_UrlPath,0,sizeof(m_UrlPath));
}

getHtml::~getHtml(void)
{
    delete m_sPageHtml;
}

bool getHtml::doWork()
{
    getPage();
    return true;
}

string getHtml::Interface()
{    
    string tempPage = "";
    try {
        SocketClient s("www.csdn.net", 80);
        s.SendLine("GET http://topic.csdn.net/u/20110608/16/9B92E542-C820-47D1-A1D8-483B4BE39B14.html");
        s.SendLine("Host: www.csdn.net");
        // s.SendLine("User-Agent	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
        // s.SendLine("Accept	*/*");
        // s.SendLine("Accept-Encoding:utf-8");
        // s.SendLine("Accept-Language:	zh-cn,zh;q=0.5");
        // s.SendLine("Accept-Charset:	GB2312");
        s.SendLine("");  

        while (1)
        {
            string html = "";
            html = s.ReceiveLine();
            if (html.empty()) break;
            tempPage += html;
        }

    } 
    catch (const char* s) {
    cerr << s << endl;
    } 
    catch (std::string s) {
    cerr << s << endl;
    } 
    catch (...) {
    cerr << "unhandled exception\n";
    }

    UTF_8ToGBK(&m_sPageHtml,(char*) tempPage.c_str());
    
    parserHtml  * ph = new parserHtml();
    ph->setPage(m_sPageHtml);
    ph->doWork();
    delete ph;
    return m_sPageHtml;
}


void getHtml::getPage()
{    
  //  cout<<m_UrlPath->shost<<'\n'<<m_UrlPath->spath<<endl;
    string tempPage = "";
    char *sPath = " ";
    char *sHost = " ";
    sprintf(sPath,"GET %s",m_UrlPath->spath);
    sprintf(sHost,"Host: %s",m_UrlPath->spath);
    try {
        SocketClient s(m_UrlPath->shost, 80);
        s.SendLine(sPath);
        s.SendLine(sHost);
        // s.SendLine("User-Agent	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
        // s.SendLine("Accept	*/*");
        // s.SendLine("Accept-Encoding:utf-8");
        // s.SendLine("Accept-Language:	zh-cn,zh;q=0.5");
        // s.SendLine("Accept-Charset:	GB2312");
        s.SendLine("");  

        while (1)
        {
            string html = "";
            html = s.ReceiveLine();
            if (html.empty()) break;
            tempPage += html;
        }

    } 
    catch (const char* s) {
    cerr << s << endl;
    } 
    catch (std::string s) {
    cerr << s << endl;
    } 
    catch (...) {
    cerr << "unhandled exception\n";
    }

    UTF_8ToGBK(&m_sPageHtml,(char*) tempPage.c_str());
    
    parserHtml  * ph = new parserHtml();
    ph->setPage(m_sPageHtml);
    ph->doWork();
    delete ph;
}

void getHtml::testPage()
{    
	string tempPage = "";
	try {
		SocketClient s("csdn.net", 80);
		s.SendLine("GET http://hi.csdn.net/fengzi2009F HTTP/1.0");
		s.SendLine("Host: csdn.net");
		// s.SendLine("User-Agent	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		// s.SendLine("Accept	*/*");
		// s.SendLine("Accept-Encoding:utf-8");
		// s.SendLine("Accept-Language:	zh-cn,zh;q=0.5");
		// s.SendLine("Accept-Charset:	GB2312");
		s.SendLine("");  

		while (1)
		{
			string html = "";
			html = s.ReceiveLine();
			if (html.empty()) break;
			tempPage += html;
		}

	} 
	catch (const char* s) {
		cerr << s << endl;
	} 
	catch (std::string s) {
		cerr << s << endl;
	} 
	catch (...) {
		cerr << "unhandled exception\n";
	}

	UTF_8ToGBK(&m_sPageHtml,(char*) tempPage.c_str());

	parserHtml  * ph = new parserHtml();
	ph->setPage(m_sPageHtml);
	ph->doWork();
	delete ph;
	//return m_sPageHtml;
}

string getHtml::putPage()
{
    //cout<< m_sPageHtml;
    return m_sPageHtml;
}